package com.lanou.beautifulwardrobe.Brand.Adapter;

import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Response;
import com.lanou.beautifulwardrobe.R;
import com.lanou.beautifulwardrobe.contants.VolleySingleton;
import com.lanou.beautifulwardrobe.entify.EditRecommendBrand;

import java.util.List;

/**
 * Created by dllo on 15/10/29.
 */
public class BrandEditAdapter extends RecyclerView.Adapter<BrandEditAdapter.EditViewHolder>{
    private List<EditRecommendBrand> datas;
    private VolleySingleton volleySingleton=VolleySingleton.getInstance();
    public void addData(List<EditRecommendBrand> datas){
        this.datas=datas;
        notifyDataSetChanged();
    }
    @Override
    public EditViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.brand_edit_item,null);
        return new EditViewHolder(v);
    }

    @Override
    public void onBindViewHolder(EditViewHolder holder, int position) {
        EditRecommendBrand brand = datas.get(position);
        holder.brandDescription.setText(brand.getDescription());
        holder.brandLikes.setText(brand.getLikes_count());
        holder.brandStyle.setText(brand.getStyle());
        getBitmap(brand.getLogo_url(), holder.brandLogoImage);
        List<String> imageUrls = brand.getTaobao_pic_urls();
        if (imageUrls != null) {
            for (int i = 0; i < imageUrls.size(); i++) {
                switch (i) {
                    case 0:
                        getBitmap(imageUrls.get(i), holder.brandFirstImage);
                        break;
                    case 1:
                        getBitmap(imageUrls.get(i), holder.brandSecondImage);
                        break;
                    default:
                        getBitmap(imageUrls.get(i), holder.brandThirdImage);
                        break;
                }
            }
        }

    }

    @Override
    public int getItemCount() {
        return datas!=null&&datas.size()>0?datas.size():0;
    }

    class EditViewHolder extends RecyclerView.ViewHolder{
        private ImageView brandFirstImage, brandSecondImage, brandThirdImage, brandLogoImage;
        private TextView brandLikes, brandDescription, brandStyle;
        public EditViewHolder(View view) {
            super(view);
            brandFirstImage = (ImageView) view.findViewById(R.id.brandFirstImage);
            brandSecondImage = (ImageView) view.findViewById(R.id.brandSecondImage);
            brandThirdImage = (ImageView) view.findViewById(R.id.brandThirdImage);
            brandLogoImage = (ImageView) view.findViewById(R.id.brandLogoImage);
            brandLikes = (TextView) view.findViewById(R.id.brandLikes);
            brandDescription = (TextView) view.findViewById(R.id.brandDescription);
            brandStyle = (TextView) view.findViewById(R.id.brandStyle);
        }
    }
    private void getBitmap(String url, final ImageView imageView) {
        volleySingleton.getImageResuest(url, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                imageView.setImageBitmap(response);
            }
        });
    }
}
